package com.oim.core.business.box;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.oim.core.app.AppContext;
import com.oim.core.bean.UserCategory;
import com.oim.core.bean.UserCategoryMember;
import com.oim.core.bean.UserData;
import com.oim.core.common.app.box.Box;
import com.oim.core.common.util.AppCommonUtil;

/**
 * @author: XiaHui
 * @date: 2016年9月28日 下午3:18:16
 */
public class UserBox extends Box {

	public UserBox(AppContext appContext) {
		super(appContext);
	}

	/** 所有用户 */
	private Map<String, UserData> allUserMap = new ConcurrentHashMap<String, UserData>();
	/** 所有分组 */
	private Map<String, UserCategory> allUserCategoryMap = new ConcurrentHashMap<String, UserCategory>();

	/** 分组中的成员列表 */
	private Map<String, Map<String, UserCategoryMember>> categoryMemberListMap = new ConcurrentHashMap<String, Map<String, UserCategoryMember>>();
	/** 用户所在的分组 */
	private Map<String, UserCategoryMember> userInCategoryMemberMap = new ConcurrentHashMap<String, UserCategoryMember>();

	/**
	 * 存放用户
	 * 
	 * @author: XiaHui
	 * @param userData
	 * @createDate: 2017年8月21日 下午5:34:02
	 * @update: XiaHui
	 * @updateDate: 2017年8月21日 下午5:34:02
	 */
	public void putUserData(UserData userData) {
		String id = userData.getId();
		UserCategoryMember ucm = this.userInCategoryMemberMap.get(id);
		if (null != ucm) {
			userData.setRemarkName(ucm.getRemark());
		}
		allUserMap.put(userData.getId(), userData);
	}

	/**
	 * 存放好友分组
	 * 
	 * @author: XiaHui
	 * @param userCategory
	 * @createDate: 2017年8月21日 下午5:34:13
	 * @update: XiaHui
	 * @updateDate: 2017年8月21日 下午5:34:13
	 */
	public void putUserCategory(UserCategory userCategory) {
		allUserCategoryMap.put(userCategory.getId(), userCategory);
	}

	/**
	 * 存放分组成员
	 * 
	 * @author: XiaHui
	 * @param userCategoryMember
	 * @createDate: 2017年8月21日 下午5:34:23
	 * @update: XiaHui
	 * @updateDate: 2017年8月21日 下午5:34:23
	 */
	public void put(UserCategoryMember userCategoryMember) {

		String memberUserId = userCategoryMember.getMemberUserId();
		String userCategoryId = userCategoryMember.getUserCategoryId();

		Map<String, UserCategoryMember> userCategoryMemberMap = getUserCategoryMemberMap(userCategoryId);
		userCategoryMemberMap.put(memberUserId, userCategoryMember);

		userInCategoryMemberMap.put(memberUserId, userCategoryMember);

		UserData userData = this.getUserData(memberUserId);
		if (null != userData) {
			userData.setRemarkName(userCategoryMember.getRemark());
		}
	}

	public void clearUserCategory() {
		allUserCategoryMap.clear();
	}

	public void clearUserCategoryMember() {
		categoryMemberListMap.clear();
		userInCategoryMemberMap.clear();
	}

	public void setUserCategoryList(List<UserCategory> userCategoryList) {
		if (null != userCategoryList) {
			for (UserCategory userCategory : userCategoryList) {
				this.putUserCategory(userCategory);
			}
		}
	}

	public void setUserDataList(List<UserData> userDataList) {
		if (null != userDataList) {
			for (UserData userData : userDataList) {
				this.putUserData(userData);
			}
		}
	}

	public void setUserCategoryMemberList(List<UserCategoryMember> userCategoryMemberList) {
		if (null != userCategoryMemberList) {
			for (UserCategoryMember userCategoryMember : userCategoryMemberList) {
				this.put(userCategoryMember);
			}
		}
	}

	/**
	 * 移除分组中的好友
	 * 
	 * @author XiaHui
	 * @date 2017年9月5日 上午10:01:44
	 * @param userId
	 * @return
	 */
	public UserCategoryMember removeUserCategoryMember(String userId) {
		UserCategoryMember userCategoryMember = userInCategoryMemberMap.remove(userId);
		if (null != userCategoryMember) {
			Map<String, UserCategoryMember> userCategoryMemberMap = getUserCategoryMemberMap(userCategoryMember.getUserCategoryId());
			userCategoryMemberMap.remove(userId);
		}
		return userCategoryMember;
	}

	/**
	 * 获取分组成员列表
	 * 
	 * @author: XiaHui
	 * @param userCategoryId
	 * @return
	 * @createDate: 2017年8月21日 下午5:36:30
	 * @update: XiaHui
	 * @updateDate: 2017年8月21日 下午5:36:30
	 */
	public List<UserCategoryMember> getUserCategoryMemberList(String userCategoryId) {
		Map<String, UserCategoryMember> userCategoryMemberMap = getUserCategoryMemberMap(userCategoryId);
		List<UserCategoryMember> list = new ArrayList<UserCategoryMember>();
		list.addAll(userCategoryMemberMap.values());
		return list;
	}

	public Map<String, UserCategoryMember> getUserCategoryMemberMap(String userCategoryId) {
		Map<String, UserCategoryMember> userCategoryMemberList = categoryMemberListMap.get(userCategoryId);
		if (null == userCategoryMemberList) {
			userCategoryMemberList = new HashMap<String, UserCategoryMember>();
			categoryMemberListMap.put(userCategoryId, userCategoryMemberList);
		}
		return userCategoryMemberList;
	}

	public int getUserCategoryMemberSize(String userCategoryId) {
		Map<String, UserCategoryMember> userCategoryMemberList = categoryMemberListMap.get(userCategoryId);
		return null == userCategoryMemberList ? 0 : userCategoryMemberList.size();
	}

	public UserCategory getUserCategory(String id) {
		return allUserCategoryMap.get(id);
	}

	public List<UserCategory> getUserCategoryList() {
		return new ArrayList<UserCategory>(allUserCategoryMap.values());
	}
	
	public UserCategoryMember getUserCategoryMemberByUserId(String userId) {
		UserCategoryMember ucm=	userInCategoryMemberMap.get(userId);
		return ucm;
	}
	
	/**
	 * 是否在好友列表
	 * 
	 * @author: XiaHui
	 * @param userId
	 * @return
	 * @createDate: 2017年8月18日 上午10:05:10
	 * @update: XiaHui
	 * @updateDate: 2017年8月18日 上午10:05:10
	 */
	public boolean inMemberList(String userId) {
		boolean has = userInCategoryMemberMap.containsKey(userId);
		return has;
	}

	public UserData getUserData(String id) {
		UserData ud = allUserMap.get(id);
		return ud;
	}

	public String getUserStatus(String id) {
		UserData ud = getUserData(id);
		return (null == ud) ? UserData.status_offline : ud.getStatus();
	}

	public boolean isOnline(String id) {
		String status = getUserStatus(id);
		boolean mark = AppCommonUtil.isOffline(status);
		return !mark;
	}
}
